package com.cs2340.taskplusplus;

import java.util.ArrayList;
import java.util.Date;

import junit.framework.Assert;
import android.test.AndroidTestCase;

// tests two of the methods of Sorter : filterComplete and filterLabel

public class JUnitAdam extends AndroidTestCase {

	Account a;
    public void testFilterComplete() throws Throwable {
		//create accounts and lists for testing purposes
		a = new Account("default");
    	a.deleteAccount("test");
    	a = new Account("test","test","test","test");
		TaskList t = a.getMasterList();
		ArrayList<Doable> results = new ArrayList<Doable>();
		
		//try with empty list
		results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.COMPLETE);
		Assert.assertTrue("empty list does not return empty list", results.isEmpty());
		
		//add a task (not done)
		Task task1 = new Task(null, "testName1", "testing testing 1", new Date("12/12/2012"), new ArrayList<Label>(), new Location(0,0));
		t.addTask(task1);
		results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.COMPLETE);
		Assert.assertTrue("when mode is COMPLETE, incomplete task is returned", results.isEmpty());
		results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.INCOMPLETE);
		Assert.assertFalse("when mode is INCOMPLETE, incomplete task is NOT returned", results.isEmpty());
		
		//switch task to done
		task1.setDone();
		results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.INCOMPLETE);
		Assert.assertTrue("when mode is INCOMPLETE, complete task is returned", results.isEmpty());
		results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.COMPLETE);
		Assert.assertFalse("when mode is COMPLETE, complete task is NOT returned", results.isEmpty());
		
		//add a second task (it starts NOT DONE)
		Task task2  = new Task(null, "testName2", "testing testing 2", new Date("12/12/2012"), new ArrayList<Label>(), new Location(0,0));
		t.addTask(task2);
		results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.COMPLETE);
		Assert.assertTrue("mode COMPLETE: returns wrong number of tasks", results.size() == 1);
		Assert.assertTrue("mode COMPLETE: returns incorrect task", results.get(0) == task1);
		results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.INCOMPLETE);
		Assert.assertTrue("mode INCOMPLETE: returns wrong number of tasks", results.size() == 1);
		Assert.assertTrue("mode INCOMPLETE: returns incorrect task", results.get(0) == task2);
		
		//**switch task 2 to DONE
		//task2.setDone();
		//**make a new task and expand it into a list (starts NOT DONE)
		//Task task3 = new Task("testParent", "testName3", "testing testing 3", new Date("12/12/2012"), new ArrayList<Label>());
		//TaskList taskList1 = task3.expand();
		//testList.add(taskList1);
		//**test it
		//results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.COMPLETE);
		//Assert.assertTrue("mode COMPLETE (now with a TaskList): returns wrong number of tasks", results.size() == 2);
		//results = (ArrayList<Doable>) Sorter.filterComplete(t.getList(), Sorter.DONEMODE.INCOMPLETE);
		//Assert.assertTrue("mode INCOMPLETE (now with a TaskList): returns incorrect TaskList", results.get(0) == taskList1);
    }

    public void testFilterLabel() throws Throwable {
		//create accounts and lists for testing purposes
		a = new Account("default");
    	a.deleteAccount("test2");
    	a = new Account("test2","test2","test2","test2");
		TaskList t = a.getMasterList();
		ArrayList<Doable> results = new ArrayList<Doable>();
		
		//make several tasks / tasklists
		Task task1 = new Task(null, "testName1", "testing testing 1", new Date("12/12/2012"), null, new Location(0,0));
		Task task2 = new Task(null, "testName2", "testing testing 2", new Date("12/12/2012"), null, new Location(0,0));
		Task task3 = new Task(null, "testName3", "testing testing 3", new Date("12/12/2012"), null, new Location(0,0));
		Task task4 = new Task(null, "testName4", "testing testing 4", new Date("12/12/2012"), null, new Location(0,0));
		//TaskList taskList2 = task2.expand();
		//TaskList taskList4 = task4.expand();
		
		//add the tasks to the TaskList
		t.addTask(task1);
		t.addTask(task2);
		t.addTask(task3);
		t.addTask(task4);
		
		//make some labels
		Label label1 = new Label(t.getTableName(), "label1");
		Label label2 = new Label(t.getTableName(), "label2");
		Label label3 = new Label(t.getTableName(), "label3");
		
		//add the labels to the tasks
		task1.addLabel(label1);
		
		task2.addLabel(label2);
		task2.addLabel(label1);
		
		task3.addLabel(label3);
		task3.addLabel(label2);
		task3.addLabel(label1);
		
		//add the labels to a list
		ArrayList<Label> allLabels = new ArrayList<Label>();
		allLabels.add(label1);
		allLabels.add(label2);
		allLabels.add(label3);
		
		//now run some tests!!
		ArrayList<Label> test  = new ArrayList<Label>();
		test.add(label1);
		results = (ArrayList<Doable>) Sorter.filterLabel(new ArrayList<Doable>(), test);
		Assert.assertTrue("empty list did not return empty list after filtering", results.isEmpty());
		
		results = (ArrayList<Doable>) Sorter.filterLabel(t.getList(), new ArrayList<Label>());
		Assert.assertTrue("no labels did not return empty", results.isEmpty());
		
		test.clear();
		test.add(label1);
		results = (ArrayList<Doable>) Sorter.filterLabel(t.getList(), test);
		Assert.assertTrue("returned wrong number of doables", results.size() == 3);
		
		test.clear();
		test.add(label2);
		results = (ArrayList<Doable>) Sorter.filterLabel(t.getList(), test);
		Assert.assertTrue("returned wrong number of doables", results.size() == 2);
		
		test.clear();
		test.add(label3);
		results = (ArrayList<Doable>) Sorter.filterLabel(t.getList(), test);
		Assert.assertTrue("returned wrong number of doables", results.size() == 1);
		
		Task task5 = new Task(null, "testName5", "testing testing 5", new Date("12/12/2012"), null, new Location(0,0));
		task5.addLabel(label3);
		t.addTask(task5);
		results = (ArrayList<Doable>) Sorter.filterLabel(t.getList(), allLabels);
		Assert.assertTrue("returned wrong number of doables", results.size() == 4);
    }
}